package com.ruoyi.material.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateCheck.domain.MateCheck;
import com.ruoyi.mateCheck.mapper.MateCheckMapper;
import com.ruoyi.material.domain.MateProductCheck;
import com.ruoyi.material.domain.MateProductCheckModel;
import com.ruoyi.material.domain.MateProductReport;
import com.ruoyi.material.mapper.CheckReportMapper;
import com.ruoyi.material.service.CheckReportService;
import com.ruoyi.mtAudit.domain.MtAudit;
import com.ruoyi.mtAudit.mapper.MtAuditMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@Service
public class CheckReportServiceImpl implements CheckReportService {

    @Autowired
    CheckReportMapper checkReportMapper;

    @Autowired
    MtAuditMapper mtAuditMapper;

    @Autowired
    MateCheckMapper mateCheckMapper;

    @Override
    public int addCheck(MateProductCheck mateProductCheck) {
        int a = 0;
        if (mateProductCheck.getMateProductCheckModels() != null && mateProductCheck.getMateCheckProducts() != null) {
            try {
                mateProductCheck.setInformationList(mateProductCheck.getOtherInformation().toString());
                a = checkReportMapper.addCheck(mateProductCheck);
                MtAudit mtAudit = new MtAudit();
                mtAudit.setCompanyId(SecurityUtils.getDeptId());
                mtAudit.setOrderId(mateProductCheck.getMateCheckId());
                mtAudit.setZjName(mateProductCheck.getZjName());
                mtAudit.setOrderType("1");
                mtAudit.setOrderNum(mateProductCheck.getOrderNum());
                mtAudit.setAuditStatus("0");
                mtAudit.setAuditBy(SecurityUtils.getLoginUser().getNickName());
                mtAuditMapper.insertMtAudit(mtAudit);
                for (MateProductCheckModel mateProductCheckModel : mateProductCheck.getMateProductCheckModels()) {
                    mateProductCheckModel.setMateCheckId(mateProductCheck.getMateCheckId());
                    mateProductCheckModel.setDeptId(SecurityUtils.getDeptId());
                    checkReportMapper.insertCheckReportModel(mateProductCheckModel);
                }
                for (MateProductReport mateProductReport : mateProductCheck.getMateCheckProducts()) {
                    mateProductReport.setDeptId(SecurityUtils.getDeptId());
                    mateProductReport.setMateCheckId(mateProductCheck.getMateCheckId());
                    checkReportMapper.insertReportProduct(mateProductReport);
                }
            } catch (Exception e) {
                a = 0;
                checkReportMapper.removeReport(mateProductCheck.getMateCheckId());
                checkReportMapper.removeReportModel(mateProductCheck.getMateCheckId());
                checkReportMapper.removeReportProduct(mateProductCheck.getMateCheckId());
                Map<String, Object> resMap = new HashMap<>();
                resMap.put("orderId", mateProductCheck.getMateCheckId());
                resMap.put("orderType", "1");
                mtAuditMapper.deleteMtAuditById(resMap);
            }
        }

        if (a > 0) {
            MateCheck mateCheck = new MateCheck();
            mateCheck.setCkInformationId(mateProductCheck.getCkInformationId());
            mateCheck.setIsCreateProject("1");
            mateCheckMapper.updateCheckInformation(mateCheck);
            return a;
        } else {
            return a;
        }
    }

    @Override
    public List<MateProductCheck> selectMateProductCheck(MateProductCheck mateProductCheck) {
        return checkReportMapper.selectMateProductCheck(mateProductCheck);
    }

    @Override
    public int updateMateProductCheck(MateProductCheck mateProductCheck) {
        int num = 0;
        if (mateProductCheck.getMateProductCheckModels() != null) {
            checkReportMapper.removeReportModel(mateProductCheck.getMateCheckId());
            for (MateProductCheckModel mateProductCheckModel : mateProductCheck.getMateProductCheckModels()) {
                mateProductCheckModel.setDeptId(SecurityUtils.getDeptId());
                mateProductCheckModel.setMateCheckId(mateProductCheck.getMateCheckId());
                checkReportMapper.insertCheckReportModel(mateProductCheckModel);
            }
            if (mateProductCheck.getMateCheckProducts() != null) {
                checkReportMapper.removeReportProduct(mateProductCheck.getMateCheckId());
                for (MateProductReport mateProductReport : mateProductCheck.getMateCheckProducts()) {
                    mateProductReport.setDeptId(SecurityUtils.getDeptId());
                    mateProductReport.setMateCheckId(mateProductCheck.getMateCheckId());
                    checkReportMapper.insertReportProduct(mateProductReport);
                }
            }
            mateProductCheck.setInformationList(mateProductCheck.getOtherInformation().toString());
            num = checkReportMapper.updateMateProductCheck(mateProductCheck);
        }
        return num;
    }

    @Override
    public void remove(Long mateCheckId, Long ckInformationId) {
        checkReportMapper.removeReportProduct(mateCheckId);
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("orderId", mateCheckId);
        resMap.put("orderType", "1");
        mtAuditMapper.deleteMtAuditById(resMap);
        checkReportMapper.removeReport(mateCheckId);
        checkReportMapper.removeReportModel(mateCheckId);
        MateCheck mateCheck = new MateCheck();
        mateCheck.setCkInformationId(ckInformationId);
        mateCheck.setIsCreateProject("0");
        mateCheckMapper.updateCheckInformation(mateCheck);

    }

    @Override
    public MateProductCheck selectMateProductCheckById(Long mateCheckId) {
        return checkReportMapper.selectMateProductCheckById(mateCheckId);
    }

    @Override
    public List<MateProductCheck> selectAuditReport(Long deptId) {
        return checkReportMapper.selectAuditReport(deptId);
    }

}
